package com.zcl.servlet;

import com.alibaba.fastjson.JSON;
import com.zcl.pojo.R;
import com.zcl.pojo.classes;
import com.zcl.service.ClassesService;
import com.zcl.service.StudentService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
    private ClassesService classesService;

    @Override
    public void init() throws ServletException {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        classesService = wac.getBean(ClassesService.class);    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        //设置字符集编码
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=utf8");
        //查询所有班级
        List<classes> classes=classesService.findAll();
        R r = R.ok().put("classes", classes);
        res.getWriter().println(JSON.toJSONString(r));
        res.getWriter().flush();
    }
}
